﻿using System;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYDuctConnectAirrTerminalElbow : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException("commandData", "is null");
				}
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				ISelectionFilter selectionFilter = new DuctSelectionFilter();
				ISelectionFilter selectionFilter2 = new DuctTerminalHorizontalSelectionFilter();
				HYDuctConnect hyductConnect = new HYDuctConnect();
				string text = "风管连接风口";
				for (;;)
				{
					RevitVersionFuncs.ClearSelection(selection);
					transaction = new Transaction(document, text);
					transaction.Start();
					Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "选择一个风管");
					Duct duct = document.GetElement(reference) as Duct;
					reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter2, "选择一个风口");
					FamilyInstance familyInstance = document.GetElement(reference) as FamilyInstance;
					ConnectHelper connectHelper = new ConnectHelper();
					Connector conn = connectHelper.GetDuctConnector(duct)[0];
					bool flag = true;
					int num = ConnectHelper.CheckPositionBetweenElement(connectHelper.GetYJKElementConnector(familyInstance), conn);
					if (1 == num || 2 == num)
					{
						flag = false;
					}
					if (flag)
					{
						if (!hyductConnect.DuctConnectAirTerminalElbow(duct, familyInstance, 0))
						{
							MessageBox.Show("连接失败，请检查风管和风口位置", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
						}
					}
					else if (1 == num)
					{
						MessageBox.Show("不能完成连接，可能原因是，风口应该处于风管的下方", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
					}
					else if (2 == num)
					{
						MessageBox.Show("不能完成连接，可能原因是，风口应该处于风管的上方", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
					}
					else
					{
						MessageBox.Show("不能完成连接，可能原因是，风口和风管上下位置不正确", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
					}
					document.Regenerate();
					transaction.Commit();
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				TransactionStatus status = transaction.GetStatus();
				if ((int)status == 1)
				{
					transaction.Commit();
				}
				else if ((int)status == 5)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
